/**
 * 
 */
package com.kylin.erp.oa.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.kylin.erp.dbmodel.TMembers;
import com.kylin.frame.core.BaseController;
import com.kylin.frame.util.ContantsUtil;
import com.kylin.frame.util.encrypt.TripleDes;

/**
 * @author huxiang
 * 
 */
public class MemberController extends BaseController {

	public void index() {

		super.render("index.html");
	}

	public void query() {

		StringBuilder where = new StringBuilder("from t_members where 1=1 ");
		List<String> params = new ArrayList<String>();
		if (!StringUtils.isBlank(getPara("cname_"))) {

			where.append("and cname_ like ? ");
			params.add("%" + getPara("cname_") + "%");
		}
		if (!StringUtils.isBlank(getPara("createdate_q"))) {
			where.append("and create_date_ >= ?");
			params.add(getPara("createdate_q") + " 00:00:00");
		}
		if (!StringUtils.isBlank(getPara("createdate_z"))) {
			where.append("and create_date_ <= ?");
			params.add(getPara("createdate_z") + " 23:59:59");
		}
		this.renderJson(TMembers.dao.paginateDataTables(getParaToInt("page")
				.intValue(), getParaToInt("rp").intValue(),
				"select id_,username_,cname_,ename_,gender_,locked_", where
						.toString(), params.toArray()));
	}

	@Before(Tx.class)
	public void add() {

		// 如果不是超级管理员，才增加
		if (!ContantsUtil._ADMINNAME.equals(getPara("username_").trim())) {

			TMembers member = TMembers.dao.findFirst(
					"select 1 from t_members where username_ = ? ",
					getPara("username_").trim());
			if (member != null) {
				this.renderJson("error", getPara("username_").trim()
						+ "已经存在，请换一个");
				return;
			}

			Map<String, Object> attrs = new HashMap<String, Object>();
			attrs.put("username_", getPara("username_").trim());
			attrs.put("password_",
					TripleDes.encrypt(getPara("password_"), null));
			attrs.put("company_id_", "kylin");
			attrs.put("cname_", getPara("cname_"));
			attrs.put("ename_", getPara("ename_"));
			attrs.put("gender_", getPara("gender_"));
			attrs.put("locked_", "N");
			attrs.put("birthday_", getPara("birthday_"));
			new TMembers().setAttrs(attrs).save();
		}

		this.renderJson("msg", "添加成功");
	}

	public void get() {

		setAttr("member", TMembers.dao.findById(this.getPara("id"))).render(
				"modifyMember.html");
	}

	@Before(Tx.class)
	public void modify() {

		// 如果不是超级管理员，才修改
		if (!ContantsUtil._ADMINNAME.equals(getPara("username_").trim())) {

			TMembers member = TMembers.dao.findFirst(
					"select 1 from t_members where username_ = ? ",
					getPara("username_").trim());
			if (member != null) {
				this.renderJson("error", getPara("username_").trim()
						+ "已经存在，请换一个");
				return;
			}

			Map<String, Object> attrs = new HashMap<String, Object>();
			attrs.put("username_", getPara("username_"));
			attrs.put("cname_", getPara("cname_"));
			attrs.put("ename_", getPara("ename_"));
			attrs.put("gender_", getPara("gender_"));
			attrs.put("birthday_", getPara("birthday_"));
			TMembers.dao.findById(getPara("id_")).setAttrs(attrs).update();
		}

		this.renderJson("msg", "修改成功");
	}

	@Before(Tx.class)
	public void resetPassword() {

		// 如果不是超级管理员，才重置
		if (!ContantsUtil._ADMINNAME.equals(getPara("username_").trim())) {
			String[] ids = this.getPara("ids").split(",");
			for (String id : ids) {
				TMembers.dao
						.findById(id)
						.set("password_",
								TripleDes.encrypt(getPara("666666"), null))
						.update();
			}
		}

		this.renderJson("msg", "重置成功");
	}

	@Before(Tx.class)
	public void lockMember() {

		if (!ContantsUtil._ADMINNAME.equals(getPara("username_").trim())) {
			String[] ids = this.getPara("ids").split(",");
			for (String id : ids) {
				TMembers.dao.findById(id).set("locked_", "Y").update();
			}
		}

		this.renderJson("msg", "锁定成功");
	}

	@Before(Tx.class)
	public void unLockMember() {

		if (!ContantsUtil._ADMINNAME.equals(getPara("username_").trim())) {
			String[] ids = this.getPara("ids").split(",");
			for (String id : ids) {
				TMembers.dao.findById(id).set("locked_", "N").update();
			}
		}

		this.renderJson("msg", "解锁成功");
	}
}
